
ALTER procedure FC_SegmentPL_Search 
@IdEntityF int,
@Year int
as
begin
select distinct IdSegmentPL, DesSegment + '\' + DesProductLine as DesSegmentPL, ICP_Number, IdEntityF, Year 
from FC_SegmentPL 
where IdEntityF =@IdEntityF and Year = @Year

end
GO


alter PROCEDURE FC_Product_Search
@IdEntityF int,
@IdSegmentPL int
as
begin
	SELECT a.IdProduct, A.CodProduct, A.DesProduct, A.CodProduct +'	- '+ A.DesProduct as CodDesproduct, A.IdEntityF, A.IdUOM, b.DesUOM, A.IdSegmentPL
	FROM FC_Product A, FC_UOM B
	WHERE A.IdUOM = b.IdUOM AND A.IdEntityF = @IdEntityF AND A.IdSegmentPL = @IdSegmentPL
end

GO

Select IdUsuario, IdPeriod, IdCustomer, IdProduct, Amount 
From FC_Forecast

--insert into FC_Forecast (IdUsuario, IdPeriod, IdCustomer, IdProduct, Amount) values (1, 22, 7, 5, 15001)

select * from FC_Product

-----------------------------------------------------------------------------


select AA.IdCustomer, AA.Codigo, AA.IdPeriod, AA.IdPeriod, AA.IdEntityF, AA.Period, ISNULL(AA.Amount,0)AS Amount, AA.DesProduct 
FROM
	(SELECT F.IdForescast, C.IdCustomer, C.Codigo, P.IdPeriod, P.IdEntityF, P.Period, P.DateClose, ISNULL(F.Amount,'') AS Amount, PR.DesProduct,
		CASE WHEN P.DateClose < DATEADD(MONTH, 2, GETDATE()) THEN 0 ELSE 1 END ACTIVE
	FROM FC_Period P 
		 LEFT JOIN FC_Customer C ON P.IdEntityF = C.IdEntityF  
		 LEFT JOIN FC_Forecast F ON F.IdPeriod = P.IdPeriod AND F.IdCustomer = C.IdCustomer 
		 LEFT JOIN FC_Product PR ON F.IdProduct = PR.IdProduct
		 LEFT JOIN FC_SegmentPL SE ON SE.IdSegmentPL = PR.IdSegmentPL	 
	WHERE 
  		(P.DateClose > DATEADD(MONTH, -10, GETDATE()) AND P.DateClose < DATEADD(MONTH, 14, GETDATE())) 
  		AND C.IdEntityF = 2) AA 
WHERE AA.IdCustomer IN (select IdCustomer from FC_Forecast)		
order by AA.Codigo	
	
